package com.eattravel.controller;

import com.eattravel.pojo.Commodity;
import com.eattravel.service.CommodityService;
import com.eattravel.vo.AreaFood;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author glong
 * @date 2019/9/24 14:34
 * 描述：
 */
@RestController
@RequestMapping("/label")
public class LabelFoodController {
    @Autowired
    private CommodityService commodityService;

    String label;

    @RequestMapping("/setLabel")
    public void setTheLabel(String theArea){
        label = theArea;
    }

    /**
     * 地方食物，模糊搜索
     * @return map集合
     */
    @RequestMapping("/labelFood1")
    @ResponseBody
    public Map<String ,Object> getLabelFood1(){
        Map<String ,Object> modelMap = new HashMap<String ,Object>();
        List<AreaFood> areaFoods = new ArrayList<>();
        // 取出查询到的数据(食品类)
        List<Commodity> list = null;
        try {
            list = commodityService.getFoodByLabel1(label);
                // 转化为vo
            for (int i=0; i<list.size(); i++){
                AreaFood food = new AreaFood();
                food.setCommodityId(list.get(i).getCommodityId());
                food.setCommodityName(list.get(i).getCommodityName());
                food.setCommodityDescribe(list.get(i).getCommodityDescribe());
                food.setPrice(list.get(i).getCommodityPrice());
                food.setPicture(list.get(i).getPictureList().get(0).getPicture());
                areaFoods.add(food);
            }
                modelMap.put("labelFood", areaFoods);
                modelMap.put("total", areaFoods.size());
        } catch (Exception e){
            e.printStackTrace();
            modelMap.put("success",true);
            modelMap.put("errMsg",e.toString());
        }
        return modelMap;
    }

    @RequestMapping("/labelFood2")
    @ResponseBody
    public Map<String ,Object> getLabelFood2(){
        Map<String ,Object> modelMap = new HashMap<String ,Object>();
        List<AreaFood> areaFoods = new ArrayList<>();
        // 取出查询到的数据(食品类)
        List<Commodity> list = null;
        try {
            list = commodityService.getFoodByLabel2(label);
            // 转化为vo
            for (int i=0; i<list.size(); i++){
                AreaFood food = new AreaFood();
                food.setCommodityId(list.get(i).getCommodityId());
                food.setCommodityName(list.get(i).getCommodityName());
                food.setCommodityDescribe(list.get(i).getCommodityDescribe());
                food.setPrice(list.get(i).getCommodityPrice());
                food.setPicture(list.get(i).getPictureList().get(0).getPicture());
                areaFoods.add(food);
            }
            modelMap.put("labelFood", areaFoods);
            modelMap.put("total", areaFoods.size());
        } catch (Exception e){
            e.printStackTrace();
            modelMap.put("success",true);
            modelMap.put("errMsg",e.toString());
        }
        return modelMap;
    }

    @RequestMapping("/labelFood3")
    @ResponseBody
    public Map<String ,Object> getLabelFood3(){
        Map<String ,Object> modelMap = new HashMap<String ,Object>();
        List<AreaFood> areaFoods = new ArrayList<>();
        // 取出查询到的数据(食品类)
        List<Commodity> list = null;
        try {
            list = commodityService.getFoodByLabel3(label);
            // 转化为vo
            for (int i=0; i<list.size(); i++){
                AreaFood food = new AreaFood();
                food.setCommodityId(list.get(i).getCommodityId());
                food.setCommodityName(list.get(i).getCommodityName());
                food.setCommodityDescribe(list.get(i).getCommodityDescribe());
                food.setPrice(list.get(i).getCommodityPrice());
                food.setPicture(list.get(i).getPictureList().get(0).getPicture());
                areaFoods.add(food);
            }
            modelMap.put("labelFood", areaFoods);
            modelMap.put("total", areaFoods.size());

        } catch (Exception e){
            e.printStackTrace();
            modelMap.put("success",true);
            modelMap.put("errMsg",e.toString());
        }
        return modelMap;
    }

    @RequestMapping("/labelFood4")
    @ResponseBody
    public Map<String ,Object> getLabelFood4(){
        Map<String ,Object> modelMap = new HashMap<String ,Object>();
        List<AreaFood> areaFoods = new ArrayList<>();
        // 取出查询到的数据(食品类)
        List<Commodity> list = null;
        try {
            list = commodityService.getFoodByLabel4(label);
            // 转化为vo
            for (int i=0; i<list.size(); i++){
                AreaFood food = new AreaFood();
                food.setCommodityId(list.get(i).getCommodityId());
                food.setCommodityName(list.get(i).getCommodityName());
                food.setCommodityDescribe(list.get(i).getCommodityDescribe());
                food.setPrice(list.get(i).getCommodityPrice());
                food.setPicture(list.get(i).getPictureList().get(0).getPicture());
                areaFoods.add(food);
            }
            modelMap.put("labelFood", areaFoods);
            modelMap.put("total", areaFoods.size());
        } catch (Exception e){
            e.printStackTrace();
            modelMap.put("success",true);
            modelMap.put("errMsg",e.toString());
        }
        return modelMap;
    }
}
